﻿using System;
using System.Linq;
using System.Reflection;

namespace NiuX;

public static class TypeExtensions
{
    /// <summary>
    /// 判断为结构体
    /// </summary>
    /// <param name="targetType"></param>
    /// <returns></returns>
    public static bool IsStruct(this Type targetType)
    {
        if (!targetType.IsPrimitive && !targetType.IsClass && !targetType.IsEnum && targetType.IsValueType)
        {
            return true;
        }
        return false;
    }

    /// <summary>
    /// 获取默认值
    /// </summary>
    /// <param name="targetType"></param>
    /// <returns></returns>
    public static object GetDefault(this Type targetType)
    {
        return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
    }

    public static PropertyInfo[] GetWriteablePropertyInfos(this Type type) => type.GetProperties().Where(x => x.CanWrite).ToArray();
    public static PropertyInfo[] GetReadablePropertyInfos(this Type type) => type.GetProperties().Where(x => x.CanRead).ToArray();
}